package com.classichu.jetpacksimple.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.classichu.jetpacksimple.R;
import com.classichu.jetpacksimple.data.local.entity.ZhihuStory;
import com.classichu.jetpacksimple.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;


public class ZhihuListRVAdapter extends RecyclerView.Adapter<ZhihuListRVAdapter.ZhihuViewHolder> {
    private List<ZhihuStory> mDataList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    @Override
    public ZhihuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ZhihuViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_zhihu_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ZhihuViewHolder holder, final int position) {
        final ZhihuStory zhihuStory = mDataList.get(position);
        holder.mTVZhihuTitle.setText(zhihuStory.getTitle());
        holder.mTVZhihuTime.setText(zhihuStory.getGa_prefix());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    return mOnItemClickListener.onItemLongClick(v, position);
                }
                return false;
            }
        });
        Glide.with(holder.itemView.getContext())
                .load(zhihuStory.getImages().get(0))
                .apply(RequestOptions.centerCropTransform())
                .into(holder.mIVZhihu);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void addDataListAtEnd(List<ZhihuStory> dataList) {
        if (dataList == null || dataList.size() == 0) {
            return;
        }
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }
    public void refreshDataList(List<ZhihuStory> dataList) {
        if (dataList == null || dataList.size() == 0) {
            return;
        }
        mDataList.clear();
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }

    class ZhihuViewHolder extends RecyclerView.ViewHolder {
        private TextView mTVZhihuTitle;
        private TextView mTVZhihuTime;
        private ImageView mIVZhihu;

        private ZhihuViewHolder(View itemView) {
            super(itemView);
            mTVZhihuTitle = (TextView) itemView.findViewById(R.id.tv_zhihu_title);
            mTVZhihuTime = (TextView) itemView.findViewById(R.id.tv_zhihu_time);
            mIVZhihu = (ImageView) itemView.findViewById(R.id.iv_zhihu);
        }
    }
}
